package com.igeek.javase.ch05.lList;

import java.util.LinkedList;

/*
 *@author wangpeng
 *2023/7/17 14:21
 *@description TODO
 */

public class LLStack {
    private LinkedList<Integer> stack;
    public LLStack(){
        stack = new LinkedList<>();
    }
    public void push(int num){
        stack.addFirst(num);;
    }
    public int pop() throws IllegalAccessException {
        if(isEmpty()){
            throw new IllegalAccessException("LLStack is empty！");
        }
        return stack.removeFirst();
    }
    public int peek() throws IllegalAccessException {
        if(isEmpty()){
            throw new IllegalAccessException("LLStack is empty!");
        }
        return stack.getFirst();
    }
    private boolean isEmpty() {
        return stack.isEmpty();
    }
    public int size(){
        return stack.size();
    }

    public static void main(String[] args) throws IllegalAccessException {
        LLStack stack = new LLStack();
        stack.push(11);
        stack.push(22);
        stack.push(33);
        stack.push(44);
        System.out.println("LLStack 容量:" + stack.size());
        System.out.println("LLStack 栈顶元素：" + stack.peek());

        while(!stack.isEmpty()){
            System.out.println("弹出元素：" + stack.pop());
        }
    }
}

